package db.access;

import java.util.HashMap;

public enum PropertyLocationEnum {
	
	GENDER("gender", "FictionalCharacter", null),
	HEIGHT("height", "FictionalCharacter", null),    	
	WEIGHT("weight", "FictionalCharacter", null),
	DATE_OF_BIRTH("standardDate", "CharacterDateOfBirth" , null),
	BASED_ON("basedOn", "BasedOn", "CharacterBasedOn"),
	OBJECTS("object", "Objects", "CharacterPossesedObject"),
	SPECIES("species", "Species", "CharacterSpecies"),
	OCCUPATION("occupation", "Occupation", "CharacterOccupation"),
	RANK("rank", "Ranks", "CharacterRank"),
	ORGANIZATION("organization", "Organization", "CharacterOrganization"),
	POWERS("power", "Powers", "CharacterPowers"),
	UNIVERSE("universe", "Universe", "CharacterUniverses"),
	CREATED_BY("createdBy", "CreatedBy", "CharacterCreatedBy"),
	ETHNICITY("ethnicity", "Ethnicity", "CharacterEthnicity"),
	SCHOOL("school", "School", "CharacterSchool"),
	MEDICAL_CONDITION("medicalCondition", "MedicalCondition", "CharacterMedicalCondition"),
	PLACES_LIVED("place", "Places", "CharacterPlacesLived"),
	PLACE_OF_BIRTH("place", "Places", "CharacterPlaceOfBirth"),
	EMPLOYER("employer", "Employer", "CharacterEmployer"),	
	PARENT("parent", "FictionalCharacter", "CharacterParent"),
	CHILDREN("child", "FictionalCharacter", "CharacterChildren"), 	
	MARRIED_TO("spouse", "FictionalCharacter", "CharacterMarriedTo"),
	ROMANTICALLY_INVOLVED_WITH("partner", "FictionalCharacter", "CharacterRomanticallyInvolvedWith"),   	
	SIBLING("sibling", "FictionalCharacter", "CharacterSibling"),
	QUOTATION("quotation", "Quotation", "CharacterQuotation");
	
	private final String property;
	private final String holdingTable;
	private final String mediatorTable;
	private final boolean isInMainTabel;
	private static final HashMap<String,PropertyLocationEnum> lookup;
	
	static {
		lookup = new HashMap<String,PropertyLocationEnum>(PropertyLocationEnum.values().length);
		for (PropertyLocationEnum propertyLocation : PropertyLocationEnum.values()) {
			if (propertyLocation.hasMediatorTable()) {
				lookup.put(propertyLocation.getMediatorTable(), propertyLocation);			
			} else if (propertyLocation.isInMainTabel) {
				lookup.put(propertyLocation.property, propertyLocation);		
			} else {
				lookup.put(propertyLocation.getHoldingTable(), propertyLocation);
			}
		}
	}
	
	public static PropertyLocationEnum get(String characterProperty) {
		return lookup.get(characterProperty);
	}

	PropertyLocationEnum(String property, String holdingTable, String mediatorTable) {
		this.property = property;
		this.holdingTable = holdingTable;
		this.mediatorTable = mediatorTable;
		this.isInMainTabel = holdingTable.equals("FictionalCharacter");
	}
	
	public String getPropertyName() {
		if (!isInMainTabel && !equals(DATE_OF_BIRTH)) {
			return property + "Name";
		} else if (hasMediatorTable()) {
			return "characterName";
		} else {
			return property;
		}
	}
	
	public String getHolderPropertyID() {
		if (equals(DATE_OF_BIRTH)) {
			return "CID";
		} else if (!isInMainTabel) {
			return property + "ID";
		} else {
			return "characterID";
		}
	}
	
	public String getMediatorPropertyID() {
		return property + "ID";
	}

	public String getHoldingTable() {
		return holdingTable;
	}

	public String getMediatorTable() {
		return mediatorTable;
	}
	
	public boolean hasMediatorTable() {
		return mediatorTable != null;
	}

}
